﻿using System;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Sys;
using VisionCloud.Domain.Repository.Interface.Sys;

namespace VisionCloud.Domain.Repository.Implement.Sys
{
    public class SysNotifyUserRepository : BaseRepository<SysNotifyUser>, ISysNotifyUserRepository
    {
        public async Task<bool> SetReaded(Guid userId, Guid notificationId)
        {
            var entity = await FirstOrDefaultAsync(x => x.UserId == userId && x.NotificationId == notificationId);
            if (entity == null)
            {
                entity = new SysNotifyUser
                {
                    CreatedDate = DateTime.Now,
                    ReadedDate = DateTime.Now,
                    IsExpired = false,
                    IsReaded = true,
                    NotificationId = notificationId,
                    UserId = userId
                };
                Add(entity);
            }
            else if (!entity.IsReaded)
            {
                entity.IsReaded = true;
                entity.ReadedDate = DateTime.Now;
                Update(entity, false);
            }
            return Db.SaveChanges() == 1;
        }
    }
}
